package Thought;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlParser {
	private File inputXml;
	private SAXReader saxReader;
	private Document document;
	private Element Items;
	private Map<String, ArrayList<String>> LIST;
	private Map<String, String> DiscountCode;

	public XmlParser(String XmlPath) throws DocumentException {
		// TODO Auto-generated constructor stub
		if (XmlPath.equals(Config.getItemXmlPath())) {
			Init(XmlPath);
			LIST = ItemsXml();
		} else {
			Init(XmlPath);
			DiscountCode = DiscountXml();
		}

	}

	private void Init(String path) throws DocumentException {
		inputXml = new File(path);
		saxReader = new SAXReader();
		document = saxReader.read(inputXml);
		Items = document.getRootElement();
	}

	private Map<String, String> DiscountXml() {
		// TODO Auto-generated method stub
		Map<String, String> tmp = new TreeMap<>();
		for (Iterator<?> i = Items.elementIterator(); i.hasNext();) {
			Element item = (Element) i.next();
			for (Iterator<?> j = item.elementIterator(); j.hasNext();) {
				Element node = (Element) j.next();
				Attribute atr = node.attribute("id");
				for (Iterator<?> k = node.elementIterator(); k.hasNext();) {
					Element code = (Element) k.next();
					tmp.put(code.getText(), atr.getText());
				}
			}
		}

		return tmp;
	}

	public Map<String, ArrayList<String>> ItemsXml() throws DocumentException {
		// TODO Auto-generated method stub

		Map<String, ArrayList<String>> list = new TreeMap<>();
		ArrayList<String> KV;
		for (Iterator<?> i = Items.elementIterator(); i.hasNext();) {
			Element item = (Element) i.next();
			KV = new ArrayList<>();
			for (Iterator<?> j = item.elementIterator(); j.hasNext();) {
				Element node = (Element) j.next();
				KV.add(node.getText());
			}
			list.put(KV.get(0), KV);
			KV.remove(0);
		}
		return list;
	}

	public void modifigXML(String oldFileName, String newFileName,
			ArrayList<String> list) throws IOException, DocumentException {
		SAXReader reader = new SAXReader();
		document = reader.read(new File(oldFileName));
		List<?> l = document.selectNodes("/xml-body/DISC/barcodes");
		Iterator<?> iter = l.iterator();
		while (iter.hasNext()) {
			Element elem = (Element) iter.next();
			Iterator<?> iterElem = elem.elementIterator("code");
			String atr = elem.attribute("id").getText();
			while (iterElem.hasNext()) {
				Element remTitle = (Element) iterElem.next();
				if (remTitle.hasContent() && atr.equals("Discount"))
					elem.remove(remTitle);
			}
			if (atr.equals("Discount")) {
				for (String str : list) {
					Element code = elem.addElement("code");
					code.setText(str);
				}
			}
		}
		FileWriter newFile = new FileWriter(new File(newFileName));
		XMLWriter newWriter = new XMLWriter(newFile);
		newWriter.write(document);
		newWriter.close();
	}

	public Map<String, ArrayList<String>> getLIST() {
		return LIST;
	}

	public void setLIST(Map<String, ArrayList<String>> lIST) {
		LIST = lIST;
	}

	public File getInputXml() {
		return inputXml;
	}

	public void setInputXml(File inputXml) {
		this.inputXml = inputXml;
	}

	public SAXReader getSaxReader() {
		return saxReader;
	}

	public void setSaxReader(SAXReader saxReader) {
		this.saxReader = saxReader;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public Element getItems() {
		return Items;
	}

	public void setItems(Element items) {
		Items = items;
	}

	public Map<String, String> getDiscountCode() {
		return DiscountCode;
	}

	public void setDiscountCode(Map<String, String> discountCode) {
		DiscountCode = discountCode;
	}

}
